package fmtprint

import (
	"fmt"
	"strconv"
)

func TestFmtPrint() {
	//printf格式输出
	name := "john"
	age := 18
	fmt.Println("name:", name, "age:", age)
	fmt.Println("name: " + name + " age: " + strconv.Itoa(age))

	fmt.Println("原样输出 %v: ")
	fmt.Printf("name: %v, age: %v\n", name, age)
	fmt.Printf("name: %#v, age: %#v\n", name, age)
	fmt.Printf("name: (%T) %v, age: (%T) %v\n", name, name, age, age)

	fmt.Println("格式化输出 %s, %d: ")
	fmt.Printf("name: %s, age: %d\n", name, age)
	fmt.Printf("name: %s, age: %+d\n", name, -age)
	fmt.Printf("name: %10s, age: %10d\n", name, age)   //右对齐，左边补空格
	fmt.Printf("name: %-10s, age: %-10d\n", name, age) //左对齐，右边补空格

	fmt.Println("不同进制的输出 %b, %o, %x: ")
	fmt.Printf("测试值：%d, 二进制：%b\n", age, age)
	fmt.Printf("测试值：%d, 八进制：%o\n", age, age)
	fmt.Printf("测试值：%d, 十六进制：%x\n", age, age)

	fmt.Println("输出格式化字符串Sprintf: ")
	s := fmt.Sprintf("name: %s, age: %d\n", name, age)
	fmt.Println("fmt.Sprintf输出值：", s)

	fmt.Println("格式化输出字符 %c %q: ")
	c := 65
	fmt.Printf("值：%d, 字符：%c\n", c, c)
	fmt.Printf("值：%d, 字符(带引号)：%q\n", c, c)

	fmt.Println("浮点数 %e %f: ")
	f := 123.456789
	fmt.Printf("科学技术法：%e\n", f)
	fmt.Printf("浮点数：%f\n", f)

	//输入
	fmt.Println("请从键盘(fmt.Scanln)输入：")
	var n string
	_, _ = fmt.Scanln(&n)
	fmt.Println("从键盘接收的值为：", n)

	fmt.Println("请从键盘输入(fmt.Scanln)你的名字和年龄：")
	var name_ string
	var age_ int
	_, _ = fmt.Scanln(&name_, &age_)
	fmt.Printf("从键盘接收的名字是：%s, 年龄是%d：\n", name_, age_)

	//通过scanf输入
	fmt.Println("请输入你的年龄和姓名:")
	fmt.Scanf("%s %d", &name_, &age_)
	fmt.Printf("从键盘接收的名字是：%s, 年龄是%d：", name_, age_)
}
